package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.api.domain.SysMsgAndTemplate;
import com.ruoyi.system.domain.SysMsgSend;
import com.ruoyi.system.domain.SysMsgTemplate;
import com.ruoyi.system.service.ISysMsgSendService;
import com.ruoyi.system.service.ISysMsgTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 消息发布Controller
 *
 * @author zmc
 * @date 2022-05-25
 */
@Api(tags = " 消息发布")
@RestController
@RequestMapping("/sysMsgSend")
public class SysMsgSendController extends BaseController {
    @Autowired
    private ISysMsgSendService sysMsgSendService;

    @Autowired
    private ISysMsgTemplateService sysMsgTemplateService;

    /**
     * 查询消息发布列表
     */
    @ApiOperation(value="查询消息发布列表", notes="查询消息发布列表")
    @PreAuthorize(hasPermi = "system:sysMsgSend:list")
    @GetMapping("/list")
    public TableDataInfo list(SysMsgSend sysMsgSend) {
        startPage();
        List<SysMsgSend> list = sysMsgSendService.selectSysMsgSendList(sysMsgSend);
        return getDataTable(list);
    }

    /**
     * 导出消息发布列表
     */
    @ApiOperation(value="导出消息发布列表", notes="导出消息发布列表")
    @PreAuthorize(hasPermi = "system:sysMsgSend:export")
    @Log(title = "消息发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMsgSend sysMsgSend) throws IOException {
        List<SysMsgSend> list = sysMsgSendService.selectSysMsgSendList(sysMsgSend);
        ExcelUtil<SysMsgSend> util = new ExcelUtil<SysMsgSend>(SysMsgSend.class);
        util.exportExcel(response, list, "消息发布数据");
    }
    /**
     * 获取消息发布详细信息
     */
    @ApiOperation(value="获取消息发布详细信息", notes="获取消息发布详细信息")
    @PreAuthorize(hasPermi = "system:sysMsgSend:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(sysMsgSendService.selectSysMsgSendById(id));
    }

    /**
     * 新增消息发布
     */
    @ApiOperation(value="新增消息发布", notes="新增消息发布")
    @PreAuthorize(hasPermi = "system:sysMsgSend:add")
    @Log(title = "消息发布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMsgSend sysMsgSend) {
        return toAjax(sysMsgSendService.insertSysMsgSend(sysMsgSend));
    }

    /**
     * 远程接口新增消息
     */
    @PostMapping(value = "/insert")
    public R insertMsg(@RequestBody SysMsgAndTemplate sysMsgSend){
        //匹配参数，生成消息并插入。
        if(StringUtils.isEmpty(sysMsgSend.getModuleType())){
            R.fail("模块类型为空");
        }
        //查询模板
        SysMsgTemplate sysMsgTemplate = new SysMsgTemplate();
        sysMsgTemplate.setModuleType(sysMsgSend.getModuleType());
        sysMsgTemplate.setStatus("正常");
        List<SysMsgTemplate> templates = sysMsgTemplateService.selectSysMsgTemplateList(sysMsgTemplate);
        if(templates ==null || templates.size()== 0){
            R.fail("模块类型对应的模板不存在或停用");
        }
        SysMsgTemplate sysMsgTemplate1 = sysMsgTemplateService.selectSysMsgTemplateById(templates.get(0).getId());
        return R.ok(sysMsgSendService.insertSysMsgSendByTemplate(sysMsgTemplate1,sysMsgSend.getParamList()));
    }

    /**
     * 远程接口新增消息
     */
    @PostMapping(value = "/pushNotice")
    public R pushNoticeMsg(){
        return R.ok(sysMsgSendService.getuiSendNoticeMsg());
    }
    /**
     * 修改消息发布
     */
    @ApiOperation(value="修改消息发布", notes="修改消息发布")
    @PreAuthorize(hasPermi = "system:sysMsgSend:edit")
    @Log(title = "消息发布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMsgSend sysMsgSend) {
        return toAjax(sysMsgSendService.updateSysMsgSend(sysMsgSend));
    }

    /**
     * 删除消息发布
     */
    @ApiOperation(value="删除消息发布", notes="删除消息发布")
    @ApiImplicitParam(name = "ids", value = "消息发布ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "system:sysMsgSend:remove")
    @Log(title = "消息发布", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sysMsgSendService.deleteSysMsgSendByIds(ids));
    }
}
